﻿using System;
using System.Text;

namespace dotHTML.HTML {
    /// <summary>
    /// Represents a &lt;script&gt; element.
    /// </summary>
    [Serializable]
    public class Script : Element {
        private bool _async;
        private string _src;
        private string _type;
        private string _language;
        private bool _defer;

        /// <summary>
        /// Constructs a new Script object.
        /// </summary>
        public Script() { }

        /// <summary>
        /// Constructs a new Script object of the given type.
        /// </summary>
        /// <param name="type">The MIME type of the encapsulating
        /// script.</param>
        public Script(string type) {
            _type = type;
        }

        /// <summary>
        /// Consturcts a new Script object with the given type and URL.
        /// </summary>
        /// <param name="type">The MIME type of the encapsulating
        /// script.</param>
        /// <param name="src">The URL to the script.</param>
        public Script(string type, string src) {
            _type = type;
            _src = src;
        }

        /// <summary>
        /// Indicates that the script should be run asynchronously with other
        /// scripts.
        /// </summary>
        public bool Async {
            get { return _async; }
            set { _async = value; }
        }

        /// <summary>
        /// Determines the URL of this script.
        /// </summary>
        public string Src {
            get { return _src; }
            set { _src = value; }
        }

        /// <summary>
        /// Determines the MIME type of this script.
        /// </summary>
        public string Type {
            get { return _type; }
            set { _type = value; }
        }

        /// <summary>
        /// Determines the language used by this script.
        /// </summary>
        [Obsolete("Deprecated - use \"Type\" instead.")]
        public string Language {
            get { return _language; }
            set { _language = value; }
        }

        /// <summary>
        /// Indicates that this script should be run after the document is
        /// parsed.
        /// </summary>
        public bool Defer {
            get { return _defer; }
            set { _defer = value; }
        }

        /// <summary>
        /// Gets a formatted string of all local attributes of this element
        /// (not including the base attributes).
        /// </summary>
        /// <param name="sb">The StringBuilder to write this element's
        /// attribute data to.</param>
        protected override void WriteLocalAttributes(StringBuilder sb) {
            AttributeWriter.Write(sb, _async, "async");
            AttributeWriter.Write(sb, _defer, "defer");
            AttributeWriter.Write(sb, _language, "language");
            AttributeWriter.Write(sb, _src, "src");
            AttributeWriter.Write(sb, _type, "type");
        }
    }
}
